package napplett1;

import java.util.ArrayList;
import java.util.Iterator;
import processing.core.*;
import napplet.*;

public class T1 extends PApplet {

    ArrayList vertices1 = new ArrayList();
    ArrayList vertices2 = new ArrayList();
    ArrayList vertices3 = new ArrayList();
    ArrayList verticesMundo1;
    ArrayList verticesMundo2;
    ArrayList verticesMundo3;
    Mundo manejadorMundo;
    Mapa manejadorMapa;
    int desX = 0, desY = 0;
    float escX = 1, escY = 1;
//    boolean dibujado = true;

    public static void main(String[] args) {
        PApplet.main(new String[]{"napplett1.T1"});
    }
    NAppletManager nappletManager;

    @Override
    public void setup() {
        size(800, 600, P2D);
        nappletManager = new NAppletManager(this);
        manejadorMundo = (Mundo) nappletManager.createNApplet("Mundo", 0, 0);
        manejadorMapa = (Mapa) nappletManager.createNApplet("Mapa", 0, 401);
    }

    @Override
    public void draw() {
        background(50);
        stroke(255);
        fill(255);
    }

    public class Mundo extends NApplet {

        Coordenada c = new Coordenada(0, 0);

        @Override
        public void setup() {
            size(800, 399);
            background(0);

        }

        @Override
        public void draw() {
//            if (dibujado) {
                pintar(escX, escY, desX, desY);
//
//            }
            
        }

        public void pintar(float escaladoX, float escaladoY, int transX, int transY) {
            stroke(250);
            background(0);
            noFill();
            verticesMundo1 = new ArrayList();
            verticesMundo2 = new ArrayList();
            verticesMundo3 = new ArrayList();
            Iterator i = vertices1.iterator();
            Iterator j = vertices2.iterator();
            Iterator k = vertices3.iterator();

            beginShape();
            while (i.hasNext()) {
                c = (Coordenada) i.next();
                c = trasladar(c, -transX, -transY);
                c = escalar(c, escaladoX, escaladoY);
                vertex(c.x, c.y);
                verticesMundo1.add(new Coordenada(c.x, c.y));
            }
            endShape();

            beginShape();
            while (j.hasNext()) {
                c = (Coordenada) j.next();
                c = trasladar(c, -transX, -transY);
                c = escalar(c, escaladoX, escaladoY);
                vertex(c.x, c.y);
                verticesMundo2.add(new Coordenada(c.x, c.y));
            }
            endShape();

            beginShape();
            while (k.hasNext()) {
                c = (Coordenada) k.next();
                c = trasladar(c, -transX, -transY);
                c = escalar(c, escaladoX, escaladoY);
                vertex(c.x, c.y);
                verticesMundo3.add(new Coordenada(c.x, c.y));
            }
            endShape();
              }
    }

    Coordenada escalar(Coordenada c, double sx, double sy) {

        double s[][] = {
            {sx, 0, 0},
            {0, sy, 0},
            {0, 0, 1}
        };
        Matrix S = new Matrix(s);
        double v[][] = {
            {c.x},
            {c.y},
            {1}
        };
        Matrix vec = new Matrix(v);
        vec = S.times(vec);
        v = vec.getData();
        c.x = (int) v[0][0];
        c.y = (int) v[1][0];

        return c;
    }

    Coordenada trasladar(Coordenada c, double dx, double dy) {
        double t[][] = {
            {1, 0, dx},
            {0, 1, dy},
            {0, 0, 1}
        };
        Matrix T = new Matrix(t);
        double v[][] = {
            {c.x},
            {c.y},
            {1}
        };
        Matrix vec = new Matrix(v);
        vec = T.times(vec);
        v = vec.getData();
        c.x = (int) v[0][0];
        c.y = (int) v[1][0];

        return c;
    }

    public class Mapa extends NApplet {

        int tamVisor;
        int yStart = 0;

        @Override
        public void setup() {
            size(800, 199);
            background(0);

        }

        @Override
        public void draw() {
            stroke(250);
            noFill();
            beginShape();
            vertex(30, 175);
            vertices1.add(new Coordenada(30, 175));
            vertex(30, 30);
            vertices1.add(new Coordenada(30, 30));
            vertex(120, 30);
            vertices1.add(new Coordenada(120, 30));
            vertex(120, 75);
            vertices1.add(new Coordenada(120, 75));
            vertex(50, 65);
            vertices1.add(new Coordenada(50, 65));
            vertex(50, 140);
            vertices1.add(new Coordenada(50, 140));
            vertex(170, 108);
            vertices1.add(new Coordenada(170, 108));
            vertex(250, 140);
            vertices1.add(new Coordenada(250, 140));
            vertex(310, 90);
            vertices1.add(new Coordenada(310, 90));
            vertex(350, 65);
            vertices1.add(new Coordenada(350, 65));
            vertex(325, 105);
            vertices1.add(new Coordenada(325, 105));
            vertex(290, 150);
            vertices1.add(new Coordenada(290, 150));
            vertex(140, 170);
            vertices1.add(new Coordenada(140, 170));
            vertex(30, 175);
            vertices1.add(new Coordenada(30, 175));
            endShape();

            beginShape();
            vertex(400, 175);
            vertices2.add(new Coordenada(400, 175));
            vertex(500, 120);
            vertices2.add(new Coordenada(500, 120));
            vertex(450, 100);
            vertices2.add(new Coordenada(450, 100));
            vertex(400, 175);
            vertices2.add(new Coordenada(400, 175));
            endShape();

            beginShape();
            vertex(600, 36);
            vertices3.add(new Coordenada(600, 36));
            vertex(750, 134);
            vertices3.add(new Coordenada(750, 134));
            endShape();

        }

        @Override
        public void mouseDragged() {

            if (mousePressed && (mouseButton == RIGHT)) {
                tamVisor = mouseY - yStart;
                escY = 399 / tamVisor;
                escX = 800 / (tamVisor * 2);
                Visor(tamVisor);
            }
            if (mousePressed && (mouseButton == LEFT)) {
                Visor(tamVisor);
                
//                dibujado = true;
            }

        }

        @Override
        public void mouseClicked() {
            if (mouseButton == LEFT) {
                yStart = mouseY;
            }
        }

        @Override
        public void mouseReleased() {
            desX = mouseX;
            desY = mouseY;
//            manejadorMundo.pintar(escX, escY, desX, desY);
        }

        public void Visor(int tam) {

            Coordenada c;
            background(0);
            stroke(250, 0, 0);
            noFill();
            beginShape();
            vertex(mouseX, mouseY);
            vertex(mouseX, (tam) + mouseY);
            vertex((2 * tam) + mouseX, (tam) + mouseY);
            vertex((2 * tam) + mouseX, mouseY);
            endShape(CLOSE);
//            Iterator i = vertices1.iterator();
//            stroke(250);
//            noFill();
//            beginShape();
//            while (i.hasNext()) {
//                c = (Coordenada) i.next();
//                c = escalar(c, 0.5, 0.5);
//                c = trasladar(c, 100, 20);
//                vertex(c.x, c.y);
//            }
//            endShape();
        }
    }

    public static class Coordenada {

        int x;
        int y;

        Coordenada(int newX, int newY) {
            x = newX;
            y = newY;
        }
    }
}
